package StudyCode.summary.mycollection;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AddsTheValuesFromTheSecondArrayToTheFirst {
	public static void main(String[] argv) {
		int[] first = new int[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		int[] second = new int[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		System.out.println(Arrays.toString(add(first, second)));
	}

	private static int[] EMPTY_INT_ARRAY = new int[0];
	private static float[] EMPTY_FLOAT_ARRAY = new float[0];


	public static int[] add(int[] first, int[] second) {
		if (first == null && second == null)
			return EMPTY_INT_ARRAY;
		if (second == null)
			return first;
		if (first == null)
			return Arrays.copyOf(second, second.length);
		if (first.length < second.length) {
			first = Arrays.copyOf(first, second.length);
		}
		int nOverlap = Math.min(first.length, second.length);
		for (int i = 0; i < nOverlap; i++) {
			first[i] += second[i];
		}
		return first;
	}

	public static float[] add(float[] first, float[] second) {
		if (first == null && second == null)
			return EMPTY_FLOAT_ARRAY;
		if (second == null)
			return first;
		if (first == null)
			return Arrays.copyOf(second, second.length);
		if (first.length < second.length) {
			first = Arrays.copyOf(first, second.length);
		}
		int nOverlap = Math.min(first.length, second.length);
		for (int i = 0; i < nOverlap; i++) {
			first[i] += second[i];
		}
		return first;
	}

	public static int[] add(int[] array, int position, int value) {
		int requiredSize = position + 1;
		if (array == null) {
			array = new int[requiredSize];
		}
		if (array.length < requiredSize) {
			array = Arrays.copyOf(array, requiredSize);
		}
		array[position] += value;
		return array;
	}

}
